In [2]:
print("Projekt wykonali:\n- Kamil Skałbania\n- Paweł Wypych\n- Miłosz Gronowski")
Projekt wykonali:
- Kamil Skałbania
- Paweł Wypych
- Miłosz Gronowski
In [3]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as stats
from scipy.stats import t, norm, chi2
from sklearn.utils import resample

data = pd.read_csv("song_data.csv")

print(data.columns)
Index(['song_name', 'song_popularity', 'song_duration_ms', 'acousticness',
       'danceability', 'energy', 'instrumentalness', 'key', 'liveness',
       'loudness', 'audio_mode', 'speechiness', 'tempo', 'time_signature',
       'audio_valence'],
      dtype='object')
In [4]:
data.info()
data.describe()

print("Dane pochodzą z platformy muzycznej Spotify, zawierają informacje o cechach akustycznych i popularności utworów.")
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 18835 entries, 0 to 18834
Data columns (total 15 columns):
 #   Column            Non-Null Count  Dtype  
---  ------            --------------  -----  
 0   song_name         18835 non-null  object 
 1   song_popularity   18835 non-null  int64  
 2   song_duration_ms  18835 non-null  int64  
 3   acousticness      18835 non-null  float64
 4   danceability      18835 non-null  float64
 5   energy            18835 non-null  float64
 6   instrumentalness  18835 non-null  float64
 7   key               18835 non-null  int64  
 8   liveness          18835 non-null  float64
 9   loudness          18835 non-null  float64
 10  audio_mode        18835 non-null  int64  
 11  speechiness       18835 non-null  float64
 12  tempo             18835 non-null  float64
 13  time_signature    18835 non-null  int64  
 14  audio_valence     18835 non-null  float64
dtypes: float64(9), int64(5), object(1)
memory usage: 2.2+ MB
Dane pochodzą z platformy muzycznej Spotify, zawierają informacje o cechach akustycznych i popularności utworów.
In [5]:
data.head(50)
Out[5]:
song_name song_popularity song_duration_ms acousticness danceability energy instrumentalness key liveness loudness audio_mode speechiness tempo time_signature audio_valence
0 Boulevard of Broken Dreams 73 262333 0.005520 0.496 0.682 0.000029 8 0.0589 -4.095 1 0.0294 167.060 4 0.4740
1 In The End 66 216933 0.010300 0.542 0.853 0.000000 3 0.1080 -6.407 0 0.0498 105.256 4 0.3700
2 Seven Nation Army 76 231733 0.008170 0.737 0.463 0.447000 0 0.2550 -7.828 1 0.0792 123.881 4 0.3240
3 By The Way 74 216933 0.026400 0.451 0.970 0.003550 0 0.1020 -4.938 1 0.1070 122.444 4 0.1980
4 How You Remind Me 56 223826 0.000954 0.447 0.766 0.000000 10 0.1130 -5.065 1 0.0313 172.011 4 0.5740
5 Bring Me To Life 80 235893 0.008950 0.316 0.945 0.000002 4 0.3960 -3.169 0 0.1240 189.931 4 0.3200
6 Last Resort 81 199893 0.000504 0.581 0.887 0.001110 4 0.2680 -3.659 0 0.0624 90.578 4 0.7240
7 Are You Gonna Be My Girl 76 213800 0.001480 0.613 0.953 0.000582 2 0.1520 -3.435 1 0.0855 105.046 4 0.5370
8 Mr. Brightside 80 222586 0.001080 0.330 0.936 0.000000 1 0.0926 -3.660 1 0.0917 148.112 4 0.2340
9 Sex on Fire 81 203346 0.001720 0.542 0.905 0.010400 9 0.1360 -5.653 1 0.0540 153.398 4 0.3740
10 The Middle 78 168253 0.042400 0.629 0.897 0.000000 2 0.2630 -3.401 1 0.0483 161.944 4 0.9300
11 Numb 63 185586 0.004600 0.496 0.863 0.000000 9 0.6390 -4.153 1 0.0381 110.018 4 0.2430
12 Smooth Criminal 75 209266 0.004340 0.647 0.964 0.003600 9 0.1500 -4.225 0 0.0600 126.942 4 0.8750
13 Can't Stop 81 269000 0.017900 0.618 0.938 0.000000 9 0.1670 -3.442 1 0.0456 91.455 4 0.8750
14 Chop Suey! 69 210240 0.000353 0.420 0.929 0.000747 7 0.1220 -3.899 0 0.1210 127.204 4 0.3000
15 Take Me Out 77 237026 0.000423 0.278 0.676 0.000899 9 0.1360 -8.821 1 0.0371 104.545 4 0.4940
16 I Miss You 71 227240 0.001360 0.659 0.778 0.000007 11 0.0841 -6.423 1 0.0379 110.022 4 0.6230
17 Best of You 62 256600 0.007010 0.370 0.944 0.000003 1 0.1350 -4.979 0 0.0767 130.315 4 0.3450
18 I Write Sins Not Tragedies 77 187613 0.093800 0.567 0.795 0.000000 9 0.1140 -4.985 0 0.1340 170.060 4 0.6350
19 Kryptonite 79 233933 0.006640 0.545 0.865 0.000011 11 0.1680 -5.708 0 0.0286 99.010 4 0.5430
20 The Kill (Bury Me) 69 231533 0.001400 0.309 0.912 0.000271 4 0.5820 -3.881 0 0.0646 183.035 3 0.3020
21 Use Somebody 79 230760 0.005520 0.276 0.715 0.000417 0 0.2010 -5.356 1 0.0432 137.028 4 0.1730
22 No One Knows 13 255066 0.013700 0.518 0.538 0.000398 0 0.1410 -5.818 1 0.0486 170.953 4 0.6870
23 Jerk It Out 62 195666 0.017100 0.580 0.981 0.000011 8 0.3860 -2.603 0 0.0600 134.007 4 0.8610
24 Uprising 77 304840 0.000202 0.602 0.905 0.064000 2 0.1170 -4.046 1 0.0775 128.019 4 0.4110
25 Hey There Delilah 79 232533 0.872000 0.657 0.291 0.000000 2 0.1140 -10.572 1 0.0293 103.973 4 0.2980
26 Blurry 28 303920 0.010200 0.440 0.932 0.000000 3 0.1420 -4.488 0 0.0584 157.438 4 0.4950
27 American Idiot 78 176346 0.000026 0.380 0.988 0.000079 1 0.3680 -2.042 1 0.0639 186.113 4 0.7690
28 Welcome to the Black Parade 77 311106 0.000289 0.217 0.905 0.000110 2 0.2220 -4.103 1 0.0752 96.950 4 0.2360
29 Gives You Hell 71 213106 0.015600 0.714 0.725 0.000000 4 0.0726 -6.411 1 0.0402 99.988 4 0.5900
30 All My Life 11 262733 0.000194 0.582 0.597 0.000273 5 0.5170 -5.671 1 0.0512 167.738 4 0.6740
31 Like a Stone 77 293960 0.007970 0.614 0.568 0.000000 7 0.0997 -5.477 0 0.0276 107.849 4 0.5160
32 It's Been Awhile 65 264706 0.001890 0.509 0.774 0.000549 6 0.1430 -4.054 1 0.0338 116.529 4 0.0824
33 I Hate Everything About You 75 231480 0.004610 0.498 0.830 0.000000 6 0.1390 -5.157 0 0.0421 89.342 4 0.4530
34 Rollin' (Air Raid Vehicle) 74 213760 0.005060 0.599 0.932 0.000000 9 0.2690 -3.328 1 0.1800 96.234 4 0.6920
35 Fat Lip 74 178266 0.000618 0.574 0.911 0.000000 9 0.0769 -5.176 1 0.0715 98.075 4 0.5730
36 The Pretender 11 269373 0.000917 0.433 0.959 0.000000 9 0.0280 -4.040 1 0.0431 172.984 4 0.3650
37 Savior 73 242280 0.001640 0.549 0.930 0.000067 5 0.3900 -3.468 0 0.0487 112.447 4 0.5260
38 Bodies 74 201960 0.003470 0.656 0.932 0.001470 4 0.1440 -3.405 0 0.0708 130.936 4 0.5440
39 Sugar, We're Goin Down 79 229093 0.008980 0.499 0.824 0.000000 7 0.1630 -4.741 1 0.0794 161.977 4 0.6990
40 Last Nite 70 193373 0.022300 0.624 0.899 0.000155 0 0.0975 -5.710 1 0.0295 104.055 4 0.7970
41 Through Glass 60 282946 0.041700 0.545 0.753 0.000000 3 0.3890 -4.618 0 0.0360 105.754 4 0.4340
42 The Diary of Jane - Single Version 69 200546 0.000055 0.374 0.961 0.083400 10 0.1380 -4.421 0 0.0851 167.032 4 0.3570
43 Down With the Sickness 73 279213 0.000996 0.695 0.876 0.000007 3 0.1060 -4.262 0 0.0553 89.954 4 0.9410
44 Wish You Were Here 62 212733 0.000631 0.246 0.768 0.301000 9 0.1020 -7.480 1 0.0387 169.873 4 0.3500
45 Youth Of The Nation 72 256240 0.008340 0.563 0.860 0.010600 8 0.3900 -7.533 1 0.0621 97.867 4 0.5170
46 Vertigo 57 194520 0.000153 0.391 0.831 0.001920 2 0.1410 -3.892 1 0.0613 139.946 4 0.6830
47 Miss Murder 71 206586 0.000213 0.309 0.875 0.001680 1 0.1130 -4.490 1 0.0488 143.526 4 0.7250
48 I Bet You Look Good On The Dancefloor 63 173680 0.002250 0.535 0.948 0.000000 6 0.3760 -4.190 0 0.0356 103.183 4 0.7780
49 Steady, As She Goes 64 215266 0.013200 0.524 0.578 0.009210 9 0.1080 -4.563 1 0.1200 123.669 4 0.5370
In [6]:
sns.pairplot(data)
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
Out[6]:
<seaborn.axisgrid.PairGrid at 0x1a828f15a90>
No description has been provided for this image
In [7]:
# Estymacja punktowa

numeric_cols = data.select_dtypes(include=np.number).columns
stats_summary = pd.DataFrame(index=numeric_cols)

stats_summary["Åšrednia"] = data[numeric_cols].mean()
stats_summary["Mediana"] = data[numeric_cols].median()
stats_summary["Moda"] = data[numeric_cols].mode().iloc[0]
stats_summary["Odchylenie std"] = data[numeric_cols].std()
stats_summary["Odchylenie przeciętne"] = data[numeric_cols].apply(lambda x: np.mean(np.abs(x - x.mean())))
stats_summary["Wariancja"] = data[numeric_cols].var()
stats_summary["Kurtoza"] = data[numeric_cols].kurt()
stats_summary["Skośność"] = data[numeric_cols].skew()
stats_summary["IQR"] = data[numeric_cols].quantile(0.75) - data[numeric_cols].quantile(0.25)
stats_summary["25%"] = data[numeric_cols].quantile(0.25)
stats_summary["75%"] = data[numeric_cols].quantile(0.75)
# porównanie między grupami( audio mode) w stosunku do acousticness   + projekty w formie pdf
display(stats_summary)
Średnia Mediana Moda Odchylenie std Odchylenie przeciętne Wariancja Kurtoza Skośność IQR 25% 75%
song_popularity 52.991877 56.000000 58.0000 21.905654 17.405513 4.798577e+02 -0.169104 -0.501487 29.00000 40.0000 69.00000
song_duration_ms 218211.587576 211306.000000 165000.0000 59887.540566 40663.192412 3.586518e+09 46.708949 3.257477 58504.50000 184339.5000 242844.00000
acousticness 0.258539 0.132000 0.1300 0.288719 0.239466 8.335861e-02 -0.096276 1.071164 0.39990 0.0241 0.42400
danceability 0.633348 0.645000 0.6870 0.156723 0.126212 2.456201e-02 -0.074797 -0.391719 0.21500 0.5330 0.74800
energy 0.644995 0.674000 0.7040 0.214101 0.174042 4.583913e-02 -0.137875 -0.620738 0.30500 0.5100 0.81500
instrumentalness 0.078008 0.000011 0.0000 0.221591 0.129948 4.910240e-02 7.563664 2.985176 0.00257 0.0000 0.00257
key 5.289196 5.000000 0.0000 3.614595 3.164517 1.306529e+01 -1.311466 -0.002520 6.00000 2.0000 8.00000
liveness 0.179650 0.122000 0.1080 0.143984 0.103522 2.073144e-02 5.789919 2.215423 0.12810 0.0929 0.22100
loudness -7.447435 -6.555000 -4.5890 3.827831 2.769083 1.465229e+01 6.522480 -1.929511 4.13600 -9.0440 -4.90800
audio_mode 0.628139 1.000000 1.0000 0.483314 0.467161 2.335928e-01 -1.718956 -0.530308 1.00000 0.0000 1.00000
speechiness 0.102099 0.055500 0.0337 0.104378 0.075313 1.089487e-02 6.504977 2.271018 0.08120 0.0378 0.11900
tempo 121.073154 120.013000 125.9780 28.714456 23.155769 8.245200e+02 -0.217517 0.442855 41.56300 98.3680 139.93100
time_signature 3.959119 4.000000 4.0000 0.298533 0.102823 8.912187e-02 45.554328 -4.979342 0.00000 4.0000 4.00000
audio_valence 0.527967 0.527000 0.9610 0.244632 0.207113 5.984466e-02 -0.977670 -0.016423 0.39000 0.3350 0.72500
In [8]:
# porównanie między grupami( audio mode) w stosunku do acousticness

acousticness_by_mode = data.groupby("audio_mode")["acousticness"].describe()
display(acousticness_by_mode)

print("Utwory w audio_mode = 1 są generalnie bardziej akustyczne niż te w audio_mode = 0, zarówno średnio, jak i w całym rozkładzie. Różnica nie jest ogromna, ale widoczna.")
count mean std min 25% 50% 75% max
audio_mode
0 7004.0 0.234486 0.269475 0.000003 0.024575 0.118 0.36025 0.996
1 11831.0 0.272778 0.298629 0.000001 0.023800 0.145 0.46250 0.996
Utwory w audio_mode = 1 są generalnie bardziej akustyczne niż te w audio_mode = 0, zarówno średnio, jak i w całym rozkładzie. Różnica nie jest ogromna, ale widoczna.
In [10]:
# 4. Estymacja przedziałowa (średnia i wariancja)
confidence = 0.95
sample = data["song_popularity"].dropna()
n = len(sample)
mean = sample.mean()
std = sample.std(ddof=1)

# Przedział ufności dla średniej
t_crit = t.ppf((1 + confidence) / 2, df=n-1)
ci_mean = (mean - t_crit * std/np.sqrt(n), mean + t_crit * std/np.sqrt(n))

# Przedział ufności dla wariancji
chi2_lower = chi2.ppf((1 - confidence) / 2, df=n-1)
chi2_upper = chi2.ppf((1 + confidence) / 2, df=n-1)
ci_var = ((n - 1) * std**2 / chi2_upper, (n - 1) * std**2 / chi2_lower)
print(f"")

print(f"Przedział ufności dla rozkładu t-studenta (średnia): ({float(ci_mean[0]):.3f}, {float(ci_mean[1]):.3f})")
print(f"Przedział ufności dla rozkładu chi2 (wariancja): ({float(ci_var[0]):.3f}, {float(ci_var[1]):.3f})")

# Estymacja bootstrap dla średniej
boot_means = [resample(sample).mean() for _ in range(1000)]
ci_bootstrap = np.percentile(boot_means, [2.5, 97.5])

print(f"Przedział bootstrap dla średniej: {ci_bootstrap}\n")

print("Z 95% pewnością (czyli poziom ufności = 0.95) prawdziwa średnia w całej populacji leży gdzieś między 52.679 a 53.305.Użyto tu rozkładu t-Studenta, ponieważ: \n- Nie znamy prawdziwego odchylenia standardowego populacji,\n- Mamy skończoną próbę (n), więc uwzględnia się większą niepewność.\n")

print("Z 95% pewnością prawdziwa wariancja w populacji (czyli rozproszenie danych) mieści się w tym zakresie (470.311, 489.699). Wariancję oszacowano za pomocą rozkładu chi-kwadrat, bo tak się postępuje przy nieznanej średniej populacji. Wariancja jest dość duża, co oznacza, że popularność piosenek jest mocno zróżnicowana.\n")

print("Bootstrap - Z 95% pewnością, prawdziwa średnia leży między 52.677 a 53.333, ale: Ten przedział nie zakłada żadnego konkretnego rozkładu (jak t-Studenta), Jest często bardziej elastyczny i odporny na odstępstwa od normalności.")
Przedział ufności dla rozkładu t-studenta (średnia): (52.679, 53.305)
Przedział ufności dla rozkładu chi2 (wariancja): (470.311, 489.699)
Przedział bootstrap dla średniej: [52.70308734 53.31458986]

Z 95% pewnością (czyli poziom ufności = 0.95) prawdziwa średnia w całej populacji leży gdzieś między 52.679 a 53.305.Użyto tu rozkładu t-Studenta, ponieważ: 
- Nie znamy prawdziwego odchylenia standardowego populacji,
- Mamy skończoną próbę (n), więc uwzględnia się większą niepewność.

Z 95% pewnością prawdziwa wariancja w populacji (czyli rozproszenie danych) mieści się w tym zakresie (470.311, 489.699). Wariancję oszacowano za pomocą rozkładu chi-kwadrat, bo tak się postępuje przy nieznanej średniej populacji. Wariancja jest dość duża, co oznacza, że popularność piosenek jest mocno zróżnicowana.

Bootstrap - Z 95% pewnością, prawdziwa średnia leży między 52.677 a 53.333, ale: Ten przedział nie zakłada żadnego konkretnego rozkładu (jak t-Studenta), Jest często bardziej elastyczny i odporny na odstępstwa od normalności.
In [11]:
# 5. Wykresy
print("Histogram pokazuje liczbę obserwacji w przedziałach wartości. KDE (linia) estymuje rozkład prawdopodobieństwa.")
print("Q-Q plot porównuje kwantyle danych do kwantyli rozkładu normalnego. Jeśli punkty układają się wzdłuż prostej → dane mają rozkład zbliżony do normalnego.")
print("Boxplot - Mediana (środkowa linia), Rozstęp międzykwartylowy (IQR) (pudełko),Wartości odstające (kropki poza wąsami).\n")

for col in numeric_cols:
    plt.figure(figsize=(14, 5))

    plt.subplot(1, 3, 1)
    sns.histplot(data[col], kde=True)
    plt.title(f'Histogram i KDE: {col}')

    plt.subplot(1, 3, 2)
    stats.probplot(data[col].dropna(), dist="norm", plot=plt)
    plt.title(f'Q-Q plot: {col}')

    plt.subplot(1, 3, 3)
    sns.boxplot(x=data[col])
    plt.title(f'Boxplot: {col}')

    plt.tight_layout()
    plt.show()
Histogram pokazuje liczbę obserwacji w przedziałach wartości. KDE (linia) estymuje rozkład prawdopodobieństwa.
Q-Q plot porównuje kwantyle danych do kwantyli rozkładu normalnego. Jeśli punkty układają się wzdłuż prostej → dane mają rozkład zbliżony do normalnego.
Boxplot - Mediana (środkowa linia), Rozstęp międzykwartylowy (IQR) (pudełko),Wartości odstające (kropki poza wąsami).

C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
In [12]:
# 6. Test normalności
from scipy.stats import normaltest

normality_results = {}
for col in numeric_cols:
    stat, p = normaltest(data[col].dropna())
    normality_results[col] = (stat, p)

normality_df = pd.DataFrame(normality_results, index=["statystyka", "p-wartość"]).T
display(normality_df)

print("Jak widać p<0.05 więc dane nie mają rozkładu normalnego.")
statystyka p-wartość
song_popularity 733.577687 5.077259e-160
song_duration_ms 15301.750221 0.000000e+00
acousticness 2477.200054 0.000000e+00
danceability 453.683117 3.047634e-99
energy 1047.297394 3.821734e-228
instrumentalness 11217.629271 0.000000e+00
key 101307.927801 0.000000e+00
liveness 8533.848559 0.000000e+00
loudness 7842.409914 0.000000e+00
audio_mode 80539.155624 0.000000e+00
speechiness 8901.039173 0.000000e+00
tempo 610.460610 2.755261e-133
time_signature 19266.571320 0.000000e+00
audio_valence 3833.366178 0.000000e+00
Jak widać p<0.05 więc dane nie mają rozkładu normalnego.
In [13]:
# 7. Testy dla średniej (t-test) i wariancji
mu_0 = 50
alpha = 0.05
mean_test = stats.ttest_1samp(data["song_popularity"].dropna(), popmean=mu_0)
var_test_stat = (n - 1) * std**2 / mu_0**2
p_val_var = 2 * min(chi2.cdf(var_test_stat, df=n-1), 1 - chi2.cdf(var_test_stat, df=n-1))

print(f"Test dla średniej (H0: µ={mu_0}): stat={mean_test.statistic}, p={mean_test.pvalue}")
print(f"Test dla wariancji (H0: σ²={mu_0**2}): stat={var_test_stat}, p={p_val_var}")

print("Średnia w danych jest statystycznie różna od 50. Tak niska p-wartość oznacza, że prawdopodobieństwo otrzymania takiego wyniku, jeśli H₀ byłaby prawdziwa, jest znikome.")
print("Statystyka testowa bardzo duża: 3615.056. p-wartość = 0.0 – interpretowana jako < 1e-16, co znów oznacza prawie zerową szansę, że taki wynik wystąpiłby przy H₀.")
print("Odrzucamy Hâ‚€.")
Test dla średniej (H0: µ=50): stat=18.744345986096672, p=1.0964098402124035e-77
Test dla wariancji (H0: σ²=2500): stat=3615.0559028617336, p=0.0
Średnia w danych jest statystycznie różna od 50. Tak niska p-wartość oznacza, że prawdopodobieństwo otrzymania takiego wyniku, jeśli H₀ byłaby prawdziwa, jest znikome.
Statystyka testowa bardzo duża: 3615.056. p-wartość = 0.0 – interpretowana jako < 1e-16, co znów oznacza prawie zerową szansę, że taki wynik wystąpiłby przy H₀.
Odrzucamy Hâ‚€.
In [14]:
# 8. Estymator jądrowy gęstości
data = data.replace([np.inf, -np.inf], np.nan)
print("KDE pokazuje jak wygląda rozkład danych — ale bez zakładania konkretnego rozkładu (np. normalnego)")
print("KDE umieszcza małą funkcję gładką (jądro – np. krzywą Gaussa) na każdym punkcie danych. Sumuje te funkcje → powstaje gładka krzywa, która przybliża rozkład danych.")

for col in numeric_cols:
    sns.kdeplot(data[col].dropna(), fill=True)
    plt.title(f'Estymator jądrowy gęstości: {col}')
    plt.xlabel(col)
    plt.show()
KDE pokazuje jak wygląda rozkład danych — ale bez zakładania konkretnego rozkładu (np. normalnego)
KDE umieszcza małą funkcję gładką (jądro – np. krzywą Gaussa) na każdym punkcie danych. Sumuje te funkcje → powstaje gładka krzywa, która przybliża rozkład danych.
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image
C:\Users\pawel\.conda\envs\tum_py313\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
No description has been provided for this image